<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('work_injury_treatments', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 关联工伤记录
            $table->unsignedBigInteger('work_injury_record_id');
            $table->string('injury_number', 50)->nullable();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 治疗信息
            $table->string('treatment_number', 50)->unique(); // 治疗编号
            $table->datetime('treatment_date'); // 治疗日期
            $table->string('treatment_type', 50); // 治疗类型：首诊、复诊、手术、康复等
            $table->string('treatment_category', 50); // 治疗类别：门诊、住院、日间手术等

            // 医院信息
            $table->string('hospital_name', 200);
            $table->string('hospital_department', 100)->nullable(); // 科室
            $table->string('doctor_name', 100)->nullable(); // 主治医生
            $table->string('treatment_location', 20)->default('local'); // 治疗地点：local, out_of_town

            // 费用信息
            $table->string('expense_type', 50); // 费用类型：住院、手术、餐饮、陪诊、开药等
            $table->decimal('expense_amount', 10, 2); // 费用金额
            $table->string('expense_source', 50); // 费用来源：company_advance, insurance_reimbursement, self_pay
            $table->string('payment_method', 50)->nullable(); // 支付方式
            $table->string('payment_reference', 100)->nullable(); // 支付凭证号

            // 保险报销信息
            $table->decimal('insurance_reimbursement_amount', 10, 2)->default(0); // 保险报销金额
            $table->decimal('self_pay_amount', 10, 2)->default(0); // 自费金额
            $table->string('insurance_policy_number', 100)->nullable(); // 保险单号
            $table->datetime('reimbursement_date')->nullable(); // 报销日期

            // 治疗详情
            $table->text('treatment_description')->nullable(); // 治疗描述
            $table->text('medical_records')->nullable(); // 病历记录
            $table->text('prescription_details')->nullable(); // 处方详情
            $table->json('medications')->nullable(); // 用药信息

            // 陪护信息
            $table->string('companion_name', 100)->nullable(); // 陪诊人姓名
            $table->string('companion_relationship', 50)->nullable(); // 与患者关系
            $table->decimal('companion_fee', 8, 2)->default(0); // 陪诊费
            $table->integer('companion_days')->default(0); // 陪诊天数

            // 状态管理
            $table->string('status', 20)->default('completed'); // pending, completed, reimbursed
            $table->boolean('is_reimbursed')->default(false); // 是否已报销
            $table->datetime('reimbursement_time')->nullable(); // 报销时间

            // 审核信息
            $table->string('approved_by', 100)->nullable();
            $table->datetime('approved_at')->nullable();
            $table->text('approval_notes')->nullable();

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable(); // 发票、处方、检查报告等

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['work_injury_record_id', 'treatment_date'], 'wit_wir_tdate_idx');
            $table->index(['employee_id', 'treatment_date'], 'wit_emp_tdate_idx');
            $table->index(['treatment_type'], 'wit_ttype_idx');
            $table->index(['expense_type'], 'wit_etype_idx');
            $table->index(['expense_source'], 'wit_esource_idx');
            $table->index(['treatment_date'], 'wit_tdate_idx');
            $table->index(['status'], 'wit_status_idx');
            $table->index(['is_reimbursed'], 'wit_reimbursed_idx');
            $table->index(['treatment_number'], 'wit_tnumber_idx');

            // 外键约束
            $table->foreign('work_injury_record_id')->references('id')->on('work_injury_records')->onDelete('cascade');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('work_injury_treatments');
    }
};
